/*
Flickr Photosets - Map Javascript
Handles the toggling of a map for a photo or photoset.

author Brad Dougherty <bdougherty@gmail.com>
license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
*/

/*
The following elements will need to be provided
before including this file:

mapLink - the link to toggle the map
mapHolder - the div that will hold the map
photo - the div to overlay with the map holder
heightElement - the element to get the height from

All events will automatically be added.

*/

var mapIn = false;
var mapInit = false;

mapLink.addEventListener('click', initMap);
mapLink.addEventListener('click', preventDefault);

function initMap(event) {
		
	// Remove initMap from onclick, replace with toggleMap
	//mapLink.removeEventListener('click', initMap);
	//mapLink.addEventListener('click', toggleMap);
	
	// Set the height of the map to match the photo
	var borderHeight =  heightElement.getOffsetHeight() - 2;
	mapHolder.setStyle('height', borderHeight + 'px');
	
	toggleMap(null);
	if (!mapInit) {
		// Get the fbml:iframe tag
		var ajax = new Ajax();
		ajax.responseType = Ajax.FBML;
		ajax.ondone = function(data) {
			mapHolder.setInnerFBML(data);
			mapInit = true;
		}
		var frameUrl = '';
		if (typeof(sid) != 'undefined') {
			frameUrl = 'uid=' + uid + '&sid=' + sid;
		}
		else {
			frameUrl = 'uid=' + uid + '&pid=' + pid;
		}
		ajax.post(callbackUrl+'/ajax.php?method=map&'+frameUrl+'&height='+borderHeight);
	}
	
	return false;
}

function hideMap() {
	Animation(mapHolder).to('opacity', 0).from(1).ease(Animation.ease.end).duration(1000).hide().checkpoint(1, function() {
		mapLink.setInnerXHTML('<span>see on a map</span>');
	}).go();
	mapIn = false;
}

function showMap() {
	Animation(mapHolder).show().to('opacity', 1).from(0).ease(Animation.ease.end).duration(1000).checkpoint(1, function() {
		mapLink.setInnerXHTML('<span>hide map</span>');
	}).go();
	mapIn = true;
}

function toggleMap(event) {
	if (mapIn) {
		hideMap();
	}
	else {
		showMap();
	}
}